package com.vma.jifu.backsys.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.vma.jifu.bizModel.model.*;
import com.vma.tools.ReturnsUtil;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by kitabi on 2017/6/16.
 */
public class CashController extends BaseController {

    public void qryCashList(){

        Integer cPage = getParaToInt("cPage");
        Integer pSize = getParaToInt("pSize");
        String name =getPara("name");
        String mystatus = getPara("status");
        String start = getPara("start");
        String end = getPara("end");

        List<String> param = new ArrayList<String>();
        String condition = "";

        if(name != null && !"".equals(name)){
            condition += "and u.nick_name like ? ";
            param.add("%"+name+"%");
        }

        if(start != null && !"".equals(start)){
            condition += "and t.create_time > ? ";
            param.add(start);
        }
        if(end != null && !"".equals(end)){
            condition += "and t.create_time < ? ";
            param.add(end);
        }
        if(mystatus != null && !"".equals(mystatus)){
            condition +="and t.status= ?";
            param.add(mystatus);
        }

        Page<Record> offer= Db.paginate(cPage,pSize,"select t.*,u.nick_name",
                " from cash_out t , user u " +
                        "where  u.id=t.user_id "+condition + " order by t.id desc ",param.toArray());

        for(int i = 0;i<offer.getList().size();i++){

            int cash_num = offer.getList().get(i).getInt("cash_num");
            DecimalFormat df = new DecimalFormat("#.00");


//            offer.getList().get(i).set("cash_num",cashnum);
            int type=  offer.getList().get(i).getInt("type");
            int user_id = offer.getList().get(i).getInt("user_id");
            if(type ==1){//支付宝
                AliAccount aliAccount = AliAccount.dao.findById(user_id);
                offer.getList().get(i).set("account_num",aliAccount.getAliAccount());
                offer.getList().get(i).set("account_name",aliAccount.getAccountName());
                offer.getList().get(i).set("type_name","支付宝");
            }else if(type ==2){//微信
                WxAccount aliAccount = WxAccount.dao.findById(user_id);
                offer.getList().get(i).set("account_num",aliAccount.getWxAccount());
                offer.getList().get(i).set("account_name",aliAccount.getAccountName());
                offer.getList().get(i).set("type_name","微信");
            }else if(type==3){//银行卡
                BankAccount aliAccount = BankAccount.dao.findById(user_id);
                offer.getList().get(i).set("account_num",aliAccount.getCardNumber());
                offer.getList().get(i).set("account_name",aliAccount.getPeopleName());
                offer.getList().get(i).set("type_name",aliAccount.getBankName());
            }
        }

        renderJson(ReturnForm(offer.getList(),offer.getTotalRow()));
    }

    public void updateStatus(){
        int mystatus = getParaToInt("status");
        String idlist = getPara("id");

        String[] ids= idlist.split(",");

        for(String id : ids){
            CashOut cash = CashOut.dao.findById(id);
            cash.setStatus(mystatus).update();
        }


        renderJson(ReturnsUtil.returnSuccess("成功"));

    }
}
